/*
** my_getnbr_base.c for my_getnbr_base in /home/goupil_r//afs/Jour_06
** 
** Made by robin goupil
** Login   <goupil_r@epitech.net>
** 
** Started on  Tue Oct  9 08:37:43 2012 robin goupil
** Last update Wed Oct 10 09:55:16 2012 robin goupil
*/

int	get_num_from_base(char *base, char c)
{
  int	j;

  j = 0;
  while (base[j] != c)
    {
      if (base[j] != '\0')
	return (-1);
      j = j + 1;
    }
  return (j);
}

int	is_params_valid(char *str, char *base)
{
  int	base_length;
  char *iterator;

  base_length = 0;
  while (str[base_length] != '\0')
    {
      base_length = base_length + 1;
    }
  if (base_length == 0)
    return (0);
  base_length = 0;
  iterator = base;
  while (base[base_length] != '\0')
  {
    while (*iterator != '\0')
      {
	if (*iterator == base[base_length])
	  return (0);
	iterator = iterator + 1;
      }
    base_length = base_length + 1;
    iterator = base;
  }
  return (base_length);
}

int	my_getnbr_base(char *str, char *base)
{
  int	i;
  int	sign;
  int	out;
  int	base_length;
  int	result;

  i = 0;
  sign = 1;
  out = 0;
  base_length = is_params_valid(str, base);
  while (str[i] != '\0' && base_length > 0)
    {
      if (str[i] == '-')
	sign = sign * (-1);
      result = get_num_from_base(base, str[i]);
      if (result < 0)
	return (0);
      out = (out * base_length) + result;
      i = i + 1;
    }
  return (out * sign);
}
